/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved.
 */
package com.kaaction.modules.operate.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.operate.entity.Client;
import com.kaaction.modules.operate.entity.ClientMaterial;
import com.kaaction.modules.operate.entity.Material;
import com.kaaction.modules.operate.service.ClientMaterialService;
import com.kaaction.modules.operate.service.ClientService;
import com.kaaction.modules.operate.service.MaterialService;

/**
 * 客户Controller
 * @author zengbiao
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/operate/client")
public class ClientController extends BaseController {

	@Autowired
	private ClientService clientService;
	
	@Autowired
	private MaterialService materialService;
	
	@Autowired
	private ClientMaterialService clientMaterialService;
	
	@ModelAttribute
	public Client get(@RequestParam(required=false) String id) {
		Client entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clientService.get(id);
		}
		if (entity == null){
			entity = new Client();
		}
		return entity;
	}
	
	@RequiresPermissions("operate:client:view")
	@RequestMapping(value = {"home"})
	public String home(Client client, Model model) {
		return "modules/operate/clientHome";
	}
	
	@RequiresPermissions("operate:client:view")
	@RequestMapping(value = {"list", ""})
	public String list(Client client, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Client> page = clientService.findPage(new Page<Client>(request, response), client); 
		model.addAttribute("page", page);
		return "modules/operate/clientList";
	}

	@RequiresPermissions("operate:client:view")
	@RequestMapping(value = "form")
	public String form(Client client, Model model) {
		
		if (!StringUtils.isBlank(client.getId())) {
			List<ClientMaterial> clientMaterialList = clientMaterialService.findListByClientId(client.getId());
			String materialIds = "";
			for(ClientMaterial ins : clientMaterialList){
				materialIds += ("," + ins.getMaterialId());
			}
			client.setMaterialIds(materialIds);;	
		}
		//生成树状结构的渠道列表
		model.addAttribute("materialList", materialService.findList(new Material()));
				
		model.addAttribute("client", client);
		return "modules/operate/clientForm";
	}

	@RequiresPermissions("operate:client:edit")
	@RequestMapping(value = "save")
	public String save(Client client, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, client)){
			return form(client, model);
		}
		
		clientService.save(client);
		if(client != null && !StringUtils.isBlank(client.getId())){
			//1.delete all related records
			clientMaterialService.deleteByClientId(client.getId());;
			
			String materialIds = client.getMaterialIds();
			//2.add 
			if(!StringUtils.isBlank(materialIds) && materialIds.indexOf(",") != -1){
				String[] materialIdArr = materialIds.split(",");
				for(String materialIdStr : materialIdArr) {
					ClientMaterial clientMaterial = new ClientMaterial();
					if(!StringUtils.isBlank(materialIdStr)){
						clientMaterial.setClientId(client.getId());
						clientMaterial.setMaterialId(materialIdStr);;
						clientMaterialService.save(clientMaterial);
					}
				}
			}
		}
		
		
		
		
		
		addMessage(redirectAttributes, "保存客户成功");
		return "redirect:"+Global.getAdminPath()+"/operate/client/?repage";
	}
	
	@RequiresPermissions("operate:client:edit")
	@RequestMapping(value = "delete")
	public String delete(Client client, RedirectAttributes redirectAttributes) {
		clientService.delete(client);
		addMessage(redirectAttributes, "删除客户成功");
		return "redirect:"+Global.getAdminPath()+"/operate/client/?repage";
	}

}